package com.wuwangfu.window;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AllWindowedStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;

/**
 * @Author jcshen
 * @Date 2023-02-23
 * @PackageName:com.wuwangfu.window
 * @ClassName: CountWindowAll
 * @Description:    GlobalWindow
 * @Version 1.0.0
 *
 * https://nightlies.apache.org/flink/flink-docs-release-1.14/docs/dev/datastream/operators/overview/#windowall
 *
 * 不分组，划分窗口
 * 按照数据的条数划分窗口，CountWindow属于GlobalWindow的一种
 *
 */
public class CountWindowAll {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        DataStreamSource<String> line = env.socketTextStream("localhost", 8888);
        //将字符串转换成数字
        SingleOutputStreamOperator<Integer> maped = line.map(Integer::parseInt);
        //划分窗口（window），按条数划分窗口，增量聚合
        //GlobalWindow并行度为1，只有一个分区（在这个分区内只有一个subtask）
        AllWindowedStream<Integer, GlobalWindow> windowed = maped.countWindowAll(5);
        //把窗口内的数据进行聚合
        SingleOutputStreamOperator<Integer> sumed = windowed.sum(0);
        sumed.print();

        env.execute();
    }
}
